﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;

namespace downLoadDataBase
{
    class Program
    {
        /// <summary>
        /// 从数据库服务器上的web上，下载rar文件，并保存到本地。
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //string url = "http://127.0.0.1/newweb/test.rar";
            string url = "http://192.168.0.2:8011/";
            string fileName = getDataFileName("newMyadData" );
            Console.WriteLine("filename=" + "databak/" + fileName + ".rar");
            Console.WriteLine("file url=" + url + fileName + ".rar");
            saveToDisk(url + fileName + ".rar", "databak/"+fileName + ".rar");

            fileName = getDataFileName("myadWorkDatabase");
            saveToDisk(url + fileName + ".rar", "databak/" + fileName + ".rar");
            // 打开文件 
            //fs = new FileStream(name, FileMode.Open, FileAccess.Read);
            //// 读取 
            //BinaryReader r = new BinaryReader(fs);
            //for (int i = 0; i < 11; i++)
            //{
            //    Console.WriteLine(r.ReadInt32());
            //}
            //w.Close();
            //fs.Close();
            Console.WriteLine("down load ok");
            //Console.ReadKey();
        }

        private static void saveToDisk(string url, string saveName)
        {
            WebClient client = new WebClient();
            //Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
            Byte[] info = client.DownloadData(url);
            
            FileStream fs = new FileStream(saveName, FileMode.CreateNew);
            BinaryWriter w = new BinaryWriter(fs);
            fs.Position = fs.Length;
            fs.Write(info, 0, info.Length);
            w.Close();
            fs.Close();
        }


        private static string getDataFileName(string dataBase)
        {
            string dwdt = DateTime.Today.ToString("yyyyMMdd");
            string fileName =  dataBase + dwdt + ".bak";
            return fileName;
        }


  

    }
}
